package com.wan.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

import com.wan.activity.util.EditTextFocusLintener;
import com.wan.commonsdk.Conet;
import com.wan.commonsdk.HttpUtils;
import com.wan.commonsdk.WanCommonSdk;
import com.wan.commonsdk.WanServerResposeCode;

import java.util.HashMap;

import net.sourceforge.simcpux.Util;
//------------------------------------------------------------------------
// 用户绑定
//
//
//------------------------------------------------------------------------

public class guest_userbind extends BaseActivity implements OnClickListener {
	private final int REGESTER_SUCCESS = WanServerResposeCode.WAN_REGESTER_SUCCESS;
	private final int REGESTER_FAILURE = WanServerResposeCode.WAN_REGESTER_FAILURE;
	private final int NETWORK_ERROR = 10086;
	private final int REGESTER_GUEST = 2;
	private final String url = "";
	private Button btn_gregester, btn_later;
	private TextView bind_back_btn;
	private EditText et_username, et_password;
	private TextView tv_userid;
	private CheckBox cb_agree, cb_showpw;
	private Thread mThread;
	private String username, password, userid, status;
	private Activity activity;
	private String commend;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		/*
		 * <EditText android:id="@+id/et_greg_username"
		 * android:layout_width="fill_parent" android:layout_height="48sp"
		 * android:padding="12dip" android:layout_gravity="top|right"
		 * android:layout_marginTop="25dp"
		 * android:background="@drawable/input_back" android:ems="10"
		 * android:hint="@string/username_hint"
		 * android:textColorHint="@color/input_text_color"
		 * android:textSize="15sp" >
		 * 
		 * <requestFocus /> </EditText>
		 * 
		 * <EditText android:id="@+id/et_greg_password"
		 */

		Util.LogUtil("GuestRegsterActivity-------onCreate");
		if (WanCommonSdk.screen_orientation == WanCommonSdk.SCREEN_ORIENTATION_PORTRAIT) {
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		} else {
			this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onCreate(savedInstanceState);
		setContentView(Util.getResID("guest_userbind", "layout"));

		activity = this;
		this.btn_gregester = (Button) findViewById(Util.getResID(
				"btn_gregester", "id"));

		btn_gregester.setOnClickListener(this);
		bind_back_btn = (TextView) findViewById(Util.getResID(
				"guestre_back_btn", "id"));

		bind_back_btn.setOnClickListener(this);

		btn_later = (Button) findViewById(Util.getResID("btn_gregester", "id"));
		btn_later.setOnClickListener(this);
		this.et_username = (EditText) findViewById(Util.getResID(
				"et_greg_username", "id"));

		this.et_password = (EditText) findViewById(Util.getResID(
				"et_greg_password", "id"));

		this.tv_userid = (TextView) findViewById(Util.getResID("tv_userid",
				"id"));

		// DBHelper dbHelper = new DBHelper(this);
		userid = Conet.mUserInfo.getUser_id();
		username = Conet.mUserInfo.getUser_name();

		this.tv_userid.setText(this.getString(Util.getResID(
				"guest_regester_userid", "string")) + userid);

		this.cb_agree = (CheckBox) findViewById(Util.getResID("cb_greg_agree",
				"id"));

		this.cb_showpw = (CheckBox) findViewById(Util.getResID(
				"cb_greg_showpw", "id"));

		cb_agree.setChecked(true);
		cb_agree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean agree) {
				// TODO Auto-generated method stub
				btn_gregester.setEnabled(agree);

			}
		});

		cb_showpw
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean showpw) {
						// TODO Auto-generated method stub
						if (showpw) {
							// ��ʾ����
							et_password
									.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
						} else {
							// ��������
							et_password.setInputType(InputType.TYPE_CLASS_TEXT
									| InputType.TYPE_TEXT_VARIATION_PASSWORD);
						}

					}
				});

		Button greg_un_edclose = (Button) findViewById(Util.getResID(
				"greg_un_edclose", "id"));
		Button greg_pw_edclose = (Button) findViewById(Util.getResID(
				"greg_un_edclose", "id"));
		et_username.setOnFocusChangeListener(new EditTextFocusLintener(
				et_username, greg_un_edclose));
		et_password.setOnFocusChangeListener(new EditTextFocusLintener(
				et_password, greg_pw_edclose));

	}

	// ------------------------------------------------------------------------
	//
	// 消息处理
	//
	// ------------------------------------------------------------------------

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {// �˷�����ui�߳�����
			switch (msg.what) {
			case REGESTER_SUCCESS:
				hideLoading();
				Conet.saveuserInfo();
				Conet.mUserInfo.setAuto_login(true);
				Conet.mUserInfo.setUser_name(username);
				Conet.mUserInfo.setUser_password(password);
				Conet.mUserInfo.setGuest(false);
				finish();
				// 游客绑定成功！！！
				break;
			case REGESTER_FAILURE:
				hideLoading();
				showHint(activity.getString(Util.getResID("login_error_hint",
						"string")));

				mThread = null;
				break;
			case WanServerResposeCode.WAN_REGESTER_WRONG_USERNAME:
				hideLoading();
				showHint("用户名已被使用");

				mThread = null;
				break;
			case WanServerResposeCode.WAN_REGESTER_WRONG_PASSWORD:
				hideLoading();
				showHint(activity.getString(Util.getResID("reg_password_hint",
						"string")));

				mThread = null;
				break;
			case WanServerResposeCode.WAN_HAS_ACCOUNT:
				hideLoading();
				if (commend != null)
					et_username.setText(commend);
				// Toast.makeText(getApplication(), "ʧ��",
				// Toast.LENGTH_LONG).show();
				showHint(activity.getString(Util.getResID("has_account_hint",
						"string")));

				mThread = null;
				break;
			case NETWORK_ERROR:
				hideLoading();
				showHint(activity.getString(Util.getResID("newwork_error",
						"string")));

				mThread = null;
				break;
			default:
				hideLoading();
				showToast(status);
				mThread = null;
				break;

			}
		}
	};

	// ------------------------------------------------------------------------
	//
	// 线程处理
	//
	// ------------------------------------------------------------------------
	Runnable runnable = new Runnable() {

		@Override
		public void run() {// run()

			HashMap<String, String> result = HttpUtils.HttpGuestReg(username,
					password, userid);
			Util.LogUtil("httpRunnable------result:" + result);
			if (result == null || result.isEmpty()) {
				mHandler.obtainMessage(NETWORK_ERROR).sendToTarget();
				return;
			} else if (result.get("status").equals(
					String.valueOf(REGESTER_SUCCESS))) {
				Conet.mUserInfo.setGuest(false);
				mHandler.obtainMessage(REGESTER_SUCCESS, Conet.mUserInfo)
						.sendToTarget();

				return;
			} else if (result.get("status").equals(
					String.valueOf(WanServerResposeCode.WAN_HAS_ACCOUNT))) {
				commend = result.get("commend");
			}
			status = result.get("status").toString();
			mHandler.obtainMessage(Integer.parseInt(result.get("status")))
					.sendToTarget();
		}
	};

	// ------------------------------------------------------------------------
	//
	// 事件处理
	//
	// ------------------------------------------------------------------------
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == Util.getResID("btn_gregester", "id")) {
			if (!checkUserInfo())
				return;
			username = this.et_username.getText().toString();
			password = this.et_password.getText().toString();
			showLoading(this.getString(Util.getResID("greg_loading_hint",
					"string")));
			mThread = new Thread(runnable);
			mThread.start();
			// Toast.makeText(this, "��¼�ɹ�", Toast.LENGTH_SHORT).show();
		} else if (v.getId() == Util.getResID("btn_greg_close", "id")) {
			this.finish();
		} else if (v.getId() == Util.getResID("btn_later", "id")) {
			finish();
		} else if (v.getId() == Util.getResID("guestre_back_btn", "id")) {
			finish();
		}
	}

	private Boolean checkUserInfo() {
		String str_pw = this.et_password.getText().toString();

		int un_length = this.et_username.getText().toString().length();
		int pw_length = str_pw.length();

		if (un_length == 0) {
			showHint(this.getString(Util.getResID("username_empty_hint",
					"string")));

			return false;
		}
		if (pw_length == 0) {
			showHint(this.getString(Util.getResID("password_empty_hint",
					"string")));

			return false;
		}

		if (un_length < WanCommonSdk.MIN_USERNAME_LENGTH
				|| un_length > WanCommonSdk.MAX_USERNAME_LENGTH) {
			showHint(this.getString(Util.getResID("username_length_hint",
					"string")));

			return false;
		}
		if (pw_length < WanCommonSdk.MIN_PASSWORD_LENGTH
				|| pw_length > WanCommonSdk.MAX_PASSWORD_LENGTH) {
			showHint(this.getString(Util.getResID("password_length_hint",
					"string")));

			return false;
		}

		return true;
	}
}
